

//
//  PhotoBrowserAnimator.m
//  kuangjia
//
//  Created by 张帅 on 15/10/14.
//  Copyright © 2015年 ZhangShuai. All rights reserved.
//

#import "PhotoBrowserAnimator.h"
#import <UIImageView+WebCache.h>
#import "HMProgressImageView.h"
#import "PhotoBrowserViewController.h"


@implementation PhotoBrowserAnimator


-(void)prepareAnimatorPicView:(StatusPictureView *)picView fromRect:(CGRect)fromRect toRect:(CGRect)toRect url:(NSURL *)url{
    
    self.picView=picView;
    self.fromRect = fromRect;
    self.toRect=toRect;
    self.url =url;

}

-(id<UIViewControllerAnimatedTransitioning>)animationControllerForDismissedController:(UIViewController *)dismissed{

    self.isPresented = NO;
    
    return self;
}

-(id<UIViewControllerAnimatedTransitioning>)animationControllerForPresentedController:(UIViewController *)presented presentingController:(UIViewController *)presenting sourceController:(UIViewController *)source{
  
    
    self.isPresented =YES;
    
    return self;

}

-(NSTimeInterval)transitionDuration:(id<UIViewControllerContextTransitioning>)transitionContext{
    return 2;
}

/// 专门实现转场动画效果 - 一旦实现了此方法，程序员必须完成动画效果
///
/// - parameter transitionContext: transition[转场]Context[上下文] 提供了转场动画所需的一切细节
-(void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext{


    PhotoBrowserViewController *fromVC  = (PhotoBrowserViewController*)[transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
    
    
    UIViewController *toView  =[transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
    
    
    self.isPresented ? [self presentAnim:transitionContext]:[self dismissAnim:transitionContext];
    
    
    NSLog(@"fromVC---------%@",fromVC);
    NSLog(@"toView%@",toView);
    
    
}


/// 实现 Modal 展现动画
///
/// - parameter transitionContext: 上下文

-(void)presentAnim:(id<UIViewControllerContextTransitioning>)transitionContext{

    
    [[transitionContext containerView ]addSubview:self.imageView];
    
    self.imageView.frame= self.fromRect;
    
    
    // 2. 用 sdwebImage 异步下载图像

    
[self.imageView sd_setImageWithURL:self.url placeholderImage:nil options:0 progress:^(NSInteger receivedSize, NSInteger expectedSize) {
    
    NSLog(@"%lf %@",receivedSize*1.0/expectedSize,[NSThread currentThread]);
    
    

    // 设置进度 - 计算下载进度

    dispatch_async(dispatch_get_main_queue(), ^{
        self.imageView.progress=  receivedSize*1.0/expectedSize;
        
    });
    
    
    
    
} completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
    
    if (error!=nil) {
        NSLog(@"%@",error);
        [transitionContext completeTransition:NO];
     return ;
    }
    
    
    [UIView animateWithDuration:2 animations:^{
        
        self.imageView.frame =self.toRect;
        
    } completion:^(BOOL finished) {
        
        UIView *toView = [transitionContext viewForKey:UITransitionContextToViewKey];
        
        [[transitionContext containerView] addSubview:toView];
       
        [self.imageView removeFromSuperview];
         // 转场动画结束后一定要声明
        [transitionContext completeTransition:YES];
        
    }];
    
    
}];
    
    

}

-(void)dismissAnim:(id<UIViewControllerContextTransitioning>)transitionContext{
    // 拿到被展现的视图 - fromView

    PhotoBrowserViewController *fromVC = (PhotoBrowserViewController*)[transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
    
    // 通过控制器获得内部的 imageView

    UIImageView *imageView =fromVC.currentImageView;
    
    NSIndexPath *indexPath = fromVC.currentImageIndex;
    
    UIView *fromView =[transitionContext viewForKey:UITransitionContextFromViewKey];
    // 直接将 fromView 从容器视图中移除

    [fromView removeFromSuperview];
     // 将图像视图添加到容器视图中
    
    [[transitionContext containerView] addSubview:imageView];
    
     imageView.center= fromView.center;
   
    
    CGFloat scale =fromVC.view.transform.a;
    
    imageView.transform = CGAffineTransformScale(imageView.transform, scale, scale);
   
    imageView.alpha=scale;

    
    NSLog(@"NSStringFromCGRect%@",NSStringFromCGRect(imageView.frame));
    
    [UIView animateWithDuration:1 animations:^{
        
        
      imageView.frame=[self.picView screenRect:indexPath];
      
    imageView.alpha=1.0;
        
        NSLog(@"animateWithDuration%@",NSStringFromCGRect(imageView.frame));
        
    } completion:^(BOOL finished) {
        

        [ imageView removeFromSuperview];
        
        [ transitionContext completeTransition:YES];
    }];


}




-(UIImageView *)imageView{
    if (_imageView==nil) {
        
        _imageView=[[HMProgressImageView alloc] init];
        
        _imageView.contentMode= UIViewContentModeScaleAspectFill;
        _imageView.clipsToBounds =YES;
    }

    return _imageView;
}


@end
